//
//  ViewController.m
//  SYS14Adapter
//
//  Created by 山刘 on 2020/8/10.
//  Copyright © 2020 山刘. All rights reserved.
//

#import "ViewController.h"
#import <Photos/Photos.h>

@interface ViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (nonatomic, strong) UIButton* photoButton;
@property (nonatomic, strong) UIButton* camaroButton;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.photoButton];
    [self.view addSubview:self.camaroButton];
    [self.photoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.top.equalTo(self.view).mas_offset(100);
    }];
    
    [self.camaroButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.photoButton);
        make.left.equalTo(self.photoButton.mas_right).mas_offset(20);
    }];
}
-(UIButton *)photoButton {
    if (!_photoButton) {
        _photoButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _photoButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [_photoButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_photoButton setTitle:@"相册" forState:UIControlStateNormal];
        [_photoButton setBackgroundColor:[UIColor greenColor]];
        [_photoButton addTarget:self action:@selector(photoButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _photoButton;
}
-(void)photoButtonClick:(UIButton*)sender{
    [self LocalPhoto];
}

-(UIButton *)camaroButton {
    if (!_camaroButton) {
        _camaroButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _camaroButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [_camaroButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_camaroButton setTitle:@"拍照" forState:UIControlStateNormal];
        [_camaroButton setBackgroundColor:[UIColor greenColor]];
        [_camaroButton addTarget:self action:@selector(camaroButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _camaroButton;
}
-(void)camaroButtonClick:(UIButton*)sender{
    [self takePhoto];
}
#pragma mark - 相册、相机功能 - iOS14
/// 拍照
-(void)takePhoto_14 {
    
}
#pragma mark - 相册、相机功能
/// 拍照
-(void)takePhoto {
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        //设置拍照后的图片可被编辑
        picker.allowsEditing = YES;
        picker.sourceType = sourceType;
        [self presentViewController:picker animated:YES completion:^{}];
    }else {
        NSLog(@"模拟其中无法打开照相机,请在真机中使用");
    }
}
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey, id> *)info {
    NSLog(@"%s",__FUNCTION__);
    [picker dismissViewControllerAnimated:YES completion:^{}];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:^{}];
}

//打开本地相册
-(void)LocalPhoto {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
 
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    //设置选择后的图片可被编辑
    picker.allowsEditing = YES;
    [self presentViewController:picker animated:YES completion:^{}];
}
@end
